//
//  HomeRecommendDetailViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/23.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "HomeRecommendDetailViewController.h"
#import "HomePushRecommendViewCell.h"
#import "HomeRecommendModel.h"
@interface HomeRecommendDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) LoadingView *load;
@property (nonatomic, assign) NSInteger page;

@end

@implementation HomeRecommendDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _datasource = [NSMutableArray arrayWithCapacity:0];
    [self setNavigationBar];
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 65, GWIDTH, GHEIGHT - 65)];
    _load.hidden = YES;
    [_load setReminderLabelTitle:@"没有更多商品" andImage:@"noNetweok"];
    [self.view addSubview:_load];
    if([GW_Request sharedInstance].noNetwork)
    {
        _load.hidden = NO;
    }
}

- (void)startNetwork
{
    
    NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
    NSString *lon = [[NSUserDefaults standardUserDefaults] objectForKey:@"long"];
    NSDictionary *param = @{@"lnt":lon, @"lat":lat, @"page":@(_page)};
    @weakobj(self);
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/goods/getHotGoods", GW_Domain] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        @strongobj(self);
        if(_page == 1)
        {
            [self.datasource removeAllObjects];
        }
        NSLog(@"爆款推荐 %@", resultDict);
        NSArray *array = resultDict[@"data"];
        for (NSDictionary *dict in array) {
            HomeRecommendModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"HomeRecommendModel"];
            [_datasource addObject:model];
        }
        
        self.page++;
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
        
        
        if (array.count < kPageSize) {
            [UIView animateWithDuration:0.3 animations:^{
                self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
            }];
        } else {
            self.tableView.mj_footer.hidden = NO;
        }
        
        if(self.datasource.count > 0)
        {
            _load.hidden = YES;
        }
        else
        {
            _load.hidden = NO;
        }
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        _load.hidden = NO;
    }];
}

//设置导航条和界面
- (void)setNavigationBar
{
    [self configNavigation:@"优选推荐"];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, GWIDTH, GHEIGHT - 65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 116;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = ColorClear;
    [_tableView registerClass:[HomePushRecommendViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(DoRefresh)];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RecommendDoAppend)];
    _tableView.mj_footer.hidden = YES;
    [_tableView.mj_header beginRefreshing];
    _page = 1;
}

- (void)DoRefresh
{
    _page = 1;
    [self startNetwork];
}

- (void)RecommendDoAppend
{
    [self startNetwork];
}

#pragma mark - UITableViewDalegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeRecommendModel *model = self.datasource[indexPath.row];
    HomePushRecommendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell configViewWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
    NSString *lon = [[NSUserDefaults standardUserDefaults] objectForKey:@"long"];
//    HomeListModel *model = self.datasource[sender.tag -1000];
    HomeRecommendModel *model = self.datasource[indexPath.row];
    [GW_webView handleWebView:[NSString stringWithFormat:@"%@/product/productDel.html?gid=%@&lat=%@&lon=%@", GW_Domain,model.gid, lat, lon] viewController:self titleName:@"首页" goodid:model.gid];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController.navigationBar setHidden:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
